package com.exchange.plugin.shiro;

import com.jfinal.aop.Duang;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.session.Session;
import org.apache.shiro.session.mgt.DefaultSessionManager;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.SimplePrincipalCollection;
import org.apache.shiro.subject.support.DefaultSubjectContext;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;

import java.util.ArrayList;
import java.util.List;

public class ShiroDbRealm extends AuthorizingRealm {
//    static final RoleService roleService = Duang.duang(RoleService.class);
//    static final MenuService menuService = Duang.duang(MenuService.class);
//    static final UserService userService = Duang.duang(UserService.class);

    //授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
		
        // 根据用户配置用户与权限
//        String account = (String) getAvailablePrincipal(principals);
//        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
//        Role role = roleService.getRoleByAccount(account);
//        if (role == null) {
//            return info;
//        }
//        System.out.println("角色:" + role.getRoleName());
//        info.addRole(role.getRoleName());
//        List<Menu> menuRoles = menuService.getAuthorizationList(role.getId());
//        List<String> lists = new ArrayList<String>();
//        for (Menu menu : menuRoles) {
//            if (menu.getPermission() != null) {
//                System.out.println("权限:" + menu.getPermission());
//                lists.add(menu.getPermission());
//            }
//        }
//        info.addStringPermissions(lists);
//        return info;
    	return null;
    }

    //身份认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken) throws AuthenticationException {
//        UsernamePasswordToken token = (UsernamePasswordToken) authcToken;
//        User user = userService.login(token.getUsername());
//        if (user == null) {
//            throw new UnknownAccountException();
//        }
//        if (user.getState() == 1) {
//            throw new LockedAccountException();
//        }
//        SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(token.getUsername(), user.getPassword(), new ByteSource(user.getSalt().getBytes()), getName());
//        return info;
    	return null;
    }

    //更新自己的授权缓存
    public void clearCached() {
        PrincipalCollection principal = SecurityUtils.getSubject().getPrincipals();
        System.out.println("清除缓存:" + principal);
        super.clearCache(principal);
    }

    /**
     * 更新全部授权缓存
     */
    public void clearAllAuthorizationCache() {
        super.getAuthorizationCache().clear();
    }

    /**
     * 更新指定用户的授权缓存
     */
    public void clearCacheAuthor(Object principal) {
        SimplePrincipalCollection info = new SimplePrincipalCollection(principal, getName());
        super.clearCachedAuthorizationInfo(info);
    }

    /**
     * 更新指定用户的认证缓存
     */
    public void clearCacheAuthen(Object principal) {
        SimplePrincipalCollection info = new SimplePrincipalCollection(principal, getName());
        System.out.println("清除认证:" + principal);
        super.clearCachedAuthenticationInfo(info);
    }

    public PrincipalCollection getPrincipal(String Account) {
        return new SimplePrincipalCollection(Account, getName());
    }

    /**
     * 剔除用户下线
     *
     * @param account
     */
    public void offline(String account) {
        DefaultWebSecurityManager securityManager = (DefaultWebSecurityManager) SecurityUtils.getSecurityManager();
        DefaultSessionManager sessionManager = (DefaultSessionManager) securityManager.getSessionManager();
        for (Session session : sessionManager.getSessionDAO().getActiveSessions()) {
            System.out.println("session:" + session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY));
            if (account.equals(session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY).toString())) {
                session.setTimeout(0L);
                System.out.println("下线");
                break;
            }
        }
    }
}
